package com.xdw.vuebackdemo.util;

/**利用redis生成自增序列
 * @author xiadewang
 */
public class SequenceUtils {

    static final int DEFAULT_PK_LENGTH = 8;   //主键的生成组合为YYMMDD+自增序列值，自增序列默认长度为8
    static final int DEFAULT_STREAM_LENGTH = 4; //直播流stream的生成策略，组合为YYMMDD+自增序列值，自增序列默认为4
    public static String getPKSequence(long seq) {
        String str = String.valueOf(seq);
        int len = str.length();
        if (len >= DEFAULT_PK_LENGTH) {// 取决于业务规模,应该不会到达4
            return str;
        }
        int rest = DEFAULT_PK_LENGTH - len;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rest; i++) {
            sb.append('0');
        }
        sb.append(str);
        return sb.toString();
    }

    public static String getStreamSequence(long seq) {
        String str = String.valueOf(seq);
        int len = str.length();
        if (len >= DEFAULT_STREAM_LENGTH) {// 取决于业务规模,应该不会到达4
            return str;
        }
        int rest = DEFAULT_STREAM_LENGTH - len;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rest; i++) {
            sb.append('0');
        }
        sb.append(str);
        return sb.toString();
    }
}
